<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/module_functions.php');
class ot_loworderfee {

	function ot_loworderfee() {
		$this->module_key = 'MODULE_ORDER_TOTAL_LOWORDERFEE';
		$this->code = 'ot_loworderfee';
		$this->enabled = ((MODULE_ORDER_TOTAL_LOWORDERFEE_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_LOWORDERFEE_SORT_ORDER;
	}

	function process($params, &$checkout_component)
	{
		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		if (MODULE_ORDER_TOTAL_LOWORDERFEE_LOW_ORDER_FEE == 'true') 
		{
			switch (MODULE_ORDER_TOTAL_LOWORDERFEE_DESTINATION) {
			case 'national':
				if ($checkout_component->shipTo['country'] == $checkout_component->shipFrom['country']) $pass = true; break;
			case 'international':
				if ($checkout_component->shipTo['country'] != $checkout_component->shipFrom['country']) $pass = true; break;
			case 'both':
				$pass = true; break;
			default:
				$pass = false; break;
			}
		}

		if ($pass == true)
		{
			$subtotal = $cart_prices[0]['value'];
			if ($subtotal < MODULE_ORDER_TOTAL_LOWORDERFEE_ORDER_UNDER)
			{
				$fee = MODULE_ORDER_TOTAL_LOWORDERFEE_FEE;
				$tax_rate = get_tax_rate(MODULE_ORDER_TOTAL_LOWORDERFEE_TAX_CLASS, $checkout_component->tax_country_id, $checkout_component->tax_zone_id);
				$tax = 0.0;
				if ($tax_rate > 0) $tax = $fee * ($tax_rate /100);
				
				$cart_prices[$this->code] = array('value' => $fee, 'title' => 'Low Order Fee:', 'class' => $this->code);
				$cart_prices['ot_tax']['value'] += $tax;
			}
		}
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Low Order Fee', 'MODULE_ORDER_TOTAL_LOWORDERFEE_STATUS', 'true', 'Do you want to display the low order fee?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_LOWORDERFEE_SORT_ORDER', '4', 'Sort order of display.', '6', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Allow Low Order Fee', 'MODULE_ORDER_TOTAL_LOWORDERFEE_LOW_ORDER_FEE', 'false', 'Do you want to allow low order fees?', '6', '3','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Order Fee For Orders Under', 'MODULE_ORDER_TOTAL_LOWORDERFEE_ORDER_UNDER', '50', 'Add the low order fee to orders under this amount.', '6', '4', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Order Fee', 'MODULE_ORDER_TOTAL_LOWORDERFEE_FEE', '5', 'Low order fee.', '6', '5', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Attach Low Order Fee On Orders Made', 'MODULE_ORDER_TOTAL_LOWORDERFEE_DESTINATION', 'both', 'Attach low order fee for orders sent to the set destination.', '6', '6', 'national, international, both', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_LOWORDERFEE_TAX_CLASS', '0', 'Use the following tax class on the low order fee.', '6', '7', 'tax_class_list()', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
